package com.huluobo.user

import com.huluobo.lib_base.BaseRepo
import com.huluobo.lib_base.Res
import com.huluobo.lib_base.entity.User
import com.huluobo.lib_base.room.UserDao
import com.huluobo.lib_http.HttpType
import com.huluobo.lib_http.NetworkFactory
import com.huluobo.user.entity.ApproveEntity
import com.huluobo.user.entity.UserEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
import javax.inject.Inject

/**
 *  Created by LC on 2025/7/30.
 */
class UserRepo @Inject constructor(private val userDao: UserDao) : BaseRepo() {
    private val apiService by lazy { NetworkFactory.factory(HttpType.NONE).create(ApiService::class.java) }
    private val apiServiceToken by lazy { NetworkFactory.factory(HttpType.TOKEN).create(ApiService::class.java) }
    private val apiServiceVideo by lazy { NetworkFactory.factory(HttpType.VIDEO).create(ApiService::class.java) }

    fun register(user: User): Flow<Long> {
        return flow { emit(userDao.register(user)) }
    }

    fun login(username: String, password: String, role: Int): Flow<User> {
        return flow { emit(userDao.login(username, password, role)) }
    }

    fun addEmployee(map: Map<String, Any>): Flow<Res<Any>> {
        return apiService.addEmployee(mapToJson(map))
    }

    fun login(map: Map<String, Any>): Flow<Res<Any>> {
        return apiService.login(mapToJson(map))
    }

    fun addApprove(map: Map<String, Any>): Flow<Res<Any>> {
        return apiServiceToken.addApprove(mapToJson(map))
    }

    fun getApprove(): Flow<Res<ApproveEntity>> {
        return apiServiceToken.getApproves()
    }

    fun deleteApprove(id: Int): Flow<Res<Any>> {
        return apiServiceToken.deleteApprove(id)
    }

    fun comApprove(map: Map<String, Any>): Flow<Res<Any>> {
        return apiServiceToken.comApprove(mapToJson(map))
    }

    suspend fun getApprovePage(page: String, limit: String): Res<ApproveEntity> {
        return withContext(Dispatchers.IO) {
            apiService.getApprovePage(page, limit)
        }
    }

    fun loginVideo(map: Map<String, Any>): Flow<Res<UserEntity>> {
        return apiServiceVideo.loginVideo(mapToJson(map))
    }
}